<?php
	// Kaikki virheilmoitukset ja varoitukset esille
	error_reporting(E_ALL);
	
	require_once("../TableGenerator/Table.php");
	require_once("../TableGenerator/Tr.php");
	require_once("../TableGenerator/Td.php");
	require_once("../TableGenerator/Th.php");
	require_once("Calendar.php");
	
	if(isset($_POST["action"]))
	{
		if($_POST["action"] == "addEvent")
		{
			$cal = new Calendar(DBConnector::DBC_MSSQL, "AEVALO", "opennh", "opennh", "OpenNeighbourhood", "Events");
			
			// check that the dates are valid
			$start = $_POST["start"];
			$end = $_POST["end"];
			// insert minutes after hour, if not present
			if(strpos($start, ':') == false)
			{
				$start .= ":00";
			}
			if(strpos($end, ':') == false)
			{
				$end .= ":00";
			}
			$sTime = trim($_POST["date"])." ".$start;
			$eTime = trim($_POST["date"])." ".$end;
			$dateStr = trim($_POST["date"])."";
			// check date
			if(strtotime($dateStr) == false)
			{
				print("Event date is incorrect!");
			}
			// check starting time
			elseif(strtotime($sTime) == false)
			{
				print("Start time is incorrect!");
			}
			// check ending time
			elseif(strtotime($eTime) == false)
			{
				print("End time is incorrect!");
			}
			// check for overlapping events
			elseif(!isset($_POST["eventID"]) && $cal->CountOverlappingEvents($sTime, $eTime) > 0)
			{
				print("This event overlaps with other events!");
			}
			elseif(isset($_POST["eventID"]) && $cal->CountOverlappingEvents($sTime, $eTime, intval($_POST["eventID"])) > 0)
			{
				print("This event overlaps with other events (edit)!");
			}
			// everything is ok, insert or update the event
			else
			{
				if(isset($_POST["eventID"]))
				{
					$ev = $cal->GetEvent(intval($_POST["eventID"]));
					$ev->EventHeader = $_POST["header"];
					$ev->EventText = $_POST["text"];
					$ev->EventType = $_POST["type"];
					$ev->EventStart = $sTime;
					$ev->EventEnd = $eTime;
					$ev->Update();
				}
				else
				{
					$cal->CreateEvent(	$_POST["header"],
										$_POST["text"],
										$_POST["type"],
										$sTime,
										$eTime);
				}
				header("Location: CalendarUI.php");
			}
		}
		elseif($_POST["action"] == "cancel")
		{
			header("Location: CalendarUI.php");
		}
	}
?>
<html>
	<head>
		<title>Add new event</title>
	</head>
	<body>
		<form action="NewEvent.php" method="post">
			<?php
				$ev = NULL;
				if(isset($_GET["editID"]))
				{
					$cal = new Calendar(DBConnector::DBC_MSSQL, "AEVALO", "opennh", "opennh", "OpenNeighbourhood", "Events");
					$ev = $cal->GetEvent(intval($_GET["editID"]));
				}


				$table = new Table();
				$table->setBorder(0);
				
				// header row
				$row = new Tr();
				$row->addColumn(new Th("Header:&nbsp;"));
				if(isset($ev))
				{
					$row->addColumn(new Td("<input type=\"text\" name=\"header\" value=\"".$ev->EventHeader."\"/>"));
				}
				else
				{
					$row->addColumn(new Td("<input type=\"text\" name=\"header\"/>"));
				}
				$table->addRow($row);
				
				// text row
				$row = new Tr();
				$row->addColumn(new Th("Text:&nbsp;"));
				if(isset($ev))
				{
					$row->addColumn(new Td("<textarea name=\"text\">".$ev->EventText."</textarea>"));
				}
				else
				{
					$row->addColumn(new Td("<textarea name=\"text\"></textarea>"));
				}
				$table->addRow($row);
				
				// type row
				$row = new Tr();
				$row->addColumn(new Th("Type:&nbsp;"));
				if(isset($ev))
				{
					$selection = "<select name=\"type\">";
					switch($ev->EventType)
					{
						case 1:
							$selection .= "<option value=\"1\" selected=\"selected\">Bileet</option>".
							"<option value=\"2\">Kokous</option>".
							"<option value=\"3\">Peli-ilta</option>";
							break;
							
						case 2:
							$selection .= "<option value=\"1\">Bileet</option>".
							"<option value=\"2\" selected=\"selected\">Kokous</option>".
							"<option value=\"3\">Peli-ilta</option>";
							break;
							
						case 3:
							$selection .= "<option value=\"1\">Bileet</option>".
							"<option value=\"2\">Kokous</option>".
							"<option value=\"3\" selected=\"selected\">Peli-ilta</option>";
							break;
					}
					$selection .= "</select>";
					$row->addColumn(new Td($selection));
				}
				else
				{
					$row->addColumn(new Td("<select name=\"type\">".
											"<option value=\"1\" selected=\"selected\">Bileet</option>".
											"<option value=\"2\">Kokous</option>".
											"<option value=\"3\">Peli-ilta</option>".
											"</select>"));
				}
				$table->addRow($row);
				
				$date = NULL;
								
				if(isset($ev))
				{
					$date = explode(" ", $ev->EventStart);
				}
				
				// date row
				$row = new Tr();
				$row->addColumn(new Th("Date (kk/pp/vvvv):&nbsp;"));
				if(isset($ev))
				{
					$row->addColumn(new Td("<input type=\"text\" name=\"date\" value=\"".$date[0]."\"/>"));
				}
				else
				{
					$row->addColumn(new Td("<input type=\"text\" name=\"date\"/>"));
				}
				$table->addRow($row);
				
				// start time
				$row = new Tr();
				$row->addColumn(new Th("Start time:&nbsp;"));
				if(isset($ev))
				{
					$row->addColumn(new Td("<input type=\"text\" name=\"start\" value=\"".$date[1]."\"/>"));
				}
				else
				{
					$row->addColumn(new Td("<input type=\"text\" name=\"start\" value=\"8:00\"/>"));
				}
				$table->addRow($row);
				
				// end time
				$row = new Tr();
				$row->addColumn(new Th("End time:&nbsp;"));
				if(isset($ev))
				{
					$endDate = explode(" ", $ev->EventEnd);
					$row->addColumn(new Td("<input type=\"text\" name=\"end\" value=\"".$endDate[1]."\"/>"));
				}
				else
				{
					$row->addColumn(new Td("<input type=\"text\" name=\"end\" value=\"9:00\"/>"));
				}
				$table->addRow($row);
				
				echo $table->getHtml();
				
				if(isset($ev))
				{
					print("<input type=\"hidden\" name=\"eventID\" value=\"".$ev->EventID."\"/>");
				}
			?>
			<button type="submit" name="action" value="addEvent">Add</button>
			<button type="submit" name="action" value="cancel">Cancel</button>
		</form>
	</body>
</html>